package com.cdel.akka.supervisor;

import com.typesafe.config.ConfigFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Props;

public class Demo {

	public static void main(String[] args) {
		ActorSystem system = ActorSystem.create("lifecycle", ConfigFactory.load("samplehello.conf"));
		customStrategy(system);
	}

	private static void customStrategy(ActorSystem system) {
		ActorRef a = system.actorOf(Props.create(Supervisor.class), "Supervisor");
		a.tell(Props.create(RestartActor.class), ActorRef.noSender());

		ActorSelection sel = system.actorSelection("akka://lifecycle/user/Supervisor/restartActor");
		for(int i=0; i<100; i++){
			sel.tell(RestartActor.Msg.RESTART, ActorRef.noSender());
		}
	}

	/**
	 *  表示RestartActor正在初始化，这时的hashcode=1537785686
	 *  preStart hashcode:1537785686
	 *  遇到空指针，将导致重启
		meet NullPointerException, restart
		preRestart在正式重启之前调用，因此hashcode=1537785686，和上一个actor还是同一个实例
		preRestart hashcode:1537785686
		进入preStart，hashcode=569835632，说明系统已经为这个RestartActor生成了新的实例，原有实例因为重启已经被回收，新的实例将代替原有的实例继续工作
		preStart hashcode:569835632
		postRestart hashcode:569835632
		meet NullPointerException, restart
		preRestart hashcode:569835632
		preStart hashcode:1400689297
		postRestart hashcode:1400689297
		meet NullPointerException, restart
		preRestart hashcode:1400689297
		preStart hashcode:765162123
		postRestart hashcode:765162123
		meet NullPointerException, restart
		postStop hashcode:765162123
		重试3次后，系统不会再进行重试，而是直接关闭RestartActor。
	 *
	 * */

}
